/*
 * Copyright 2014 Cungmua, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cungmua.vn.kiosk.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.text.Normalizer;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.support.v4.app.DialogFragment;
import android.text.format.DateFormat;
import android.view.Window;
import android.widget.TimePicker;

/**
 * @author kelvin.nguyen
 * 
 */
public class Utils {
	public static String TAG = "Kiosk";
	public static final int DH_STATUS_RESTORE = 10;
	public static final int DH_STATUS_SUCCESS = 7;
	public static final int DH_STATUS_DELAY = 1;
	public static final int DH_STATUS_CONTACT_FAILED = 0;
	public static final int DH_STATUS_WRONG_PRODUCT = 3;
	public static final int DH_STATUS_CANCEL = 2;
	
	public static final int SKU_STATUS_SUCCESS = 1;
	
	public static final int SKU_STATUS_CANCEL = 0;
	
	public static final int DH_SYNCHONIZED = 1;
	public static final int DH_SYNCHONIZE = 0;

	public static void requestWindowFeature(Activity ac) {
		ac.requestWindowFeature(Window.FEATURE_NO_TITLE);
	}

	public static String priceWithoutDecimal(Double price) {
		DecimalFormat formatter = new DecimalFormat("###,###,###.##");
		return formatter.format(price);
	}

	public static String formateDate(String before_date) {
		String date_after = formateDateFromstring("yyyy-MM-dd",
				"dd, MMM yyyy", before_date);
		return date_after;
	}

	public static String formateDateFromstring(String inputFormat,
			String outputFormat, String inputDate) {

		Date parsed = null;
		String outputDate = "";

		SimpleDateFormat df_input = new SimpleDateFormat(inputFormat,
				java.util.Locale.getDefault());
		SimpleDateFormat df_output = new SimpleDateFormat(outputFormat,
				java.util.Locale.getDefault());

		try {
			parsed = df_input.parse(inputDate);
			outputDate = df_output.format(parsed);

		} catch (ParseException e) {

		}

		return outputDate;

	}


		

	public static final String md5(final String s) {
		final String MD5 = "MD5";
		try {
			// Create MD5 Hash
			MessageDigest digest = java.security.MessageDigest.getInstance(MD5);
			digest.update(s.getBytes());
			byte messageDigest[] = digest.digest();

			// Create Hex String
			StringBuilder hexString = new StringBuilder();
			for (byte aMessageDigest : messageDigest) {
				String h = Integer.toHexString(0xFF & aMessageDigest);
				while (h.length() < 2)
					h = "0" + h;
				hexString.append(h);
			}
			return hexString.toString();

		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return "";
	}

	public static final String getDeviceId(Context c) {
		return Secure.getString(c.getContentResolver(), Secure.ANDROID_ID);
	}

	public static String sha256(String base) {
		try {
			MessageDigest digest = MessageDigest.getInstance("SHA-256");
			byte[] hash = digest.digest(base.getBytes("UTF-8"));
			StringBuffer hexString = new StringBuffer();

			for (int i = 0; i < hash.length; i++) {
				String hex = Integer.toHexString(0xff & hash[i]);
				if (hex.length() == 1)
					hexString.append('0');
				hexString.append(hex);
			}

			return hexString.toString();
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		}
	}

	public static class TimePickerFragment extends DialogFragment implements
			TimePickerDialog.OnTimeSetListener {

		@Override
		public Dialog onCreateDialog(Bundle savedInstanceState) {
			// Use the current time as the default values for the picker
			final Calendar c = Calendar.getInstance();
			int hour = c.get(Calendar.HOUR_OF_DAY);
			int minute = c.get(Calendar.MINUTE);

			// Create a new instance of TimePickerDialog and return it
			return new TimePickerDialog(getActivity(), this, hour, minute,
					DateFormat.is24HourFormat(getActivity()));
		}

		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			// Do something with the time chosen by the user
		}
	}

	public static String getDayOfWeek(int year, int month, int day) {
		Calendar cal = Calendar.getInstance();
		cal.set(year, month, day);
		String dayOfWeek = null;
		switch (cal.get(Calendar.DAY_OF_WEEK)) {
		case 1:
			dayOfWeek = "CN";
			break;
		case 2:
			dayOfWeek = "Th 2";
			break;
		case 3:
			dayOfWeek = "Th 3";
			break;
		case 4:
			dayOfWeek = "Th 4";
			break;
		case 5:
			dayOfWeek = "Th 5";
			break;
		case 6:
			dayOfWeek = "Th 6";
			break;
		case 7:
			dayOfWeek = "Th 7";

			break;
		}
		return dayOfWeek;
	}

}
